#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Standalone Source Helper ===
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_gerrit.rc)

# === Gerrit Review Uploader ===
function gerritreview()
{
  # Variables
  local gerrit_url=${1};
  local gerrit_organisation=${2};
  local gerrit_type=${3};
  local gerrit_branch=${4:-$(repogetbranch)};
  local gerrit_confirm_twice=;
  local gerrit_project=;
  local gerrit_remote=;

  # Handle gerrit commit types
  case "${gerrit_type}" in
    drafts)
      ;;
    for|heads)
      gerrit_confirm_twice=true;;
    *)
      gerrit_type=;;
  esac;

  # Usage
  if [ -z "${gerrit_url}" ] || [ -z "${gerrit_organisation}" ] || [ -z "${gerrit_type}" ] || [ -z "${gerrit_branch}" ]; then
    echo '';
    echo ' Usage: gerritreview <gerrit_ssh_or_http> <project_name_or_.> <drafts/for/heads> [branch] (Gerrit review uploader)';
    echo '';
    return;
  fi;

  # Remote detection
  if [ ! "${gerrit_organisation}" = '.' ]; then
    gerrit_remote=$(git remote -v | grep "/${gerrit_organisation}/" | tail -n 1);
  fi;
  if [ -z "${gerrit_remote}" ] && [ ! "${gerrit_organisation}" = 'aosp' ]; then
    gerrit_remote=$(git remote -v | grep origin$'\t' | tail -n 1);
  fi;
  if [ -z "${gerrit_remote}" ]; then
    gerrit_remote=$(git remote -v | grep aosp$'\t' | tail -n 1);
  fi;
  if [ -z "${gerrit_remote}" ]; then
    gerrit_remote=$(git remote -v | grep "${gerrit_organisation}.*//" | tail -n 1);
  fi;

  # Project detection for AOSP
  if [ "${gerrit_organisation}" = 'aosp' ]; then
    gerrit_project=$(echo "${gerrit_remote}" \
                   | cut -f 2 \
                   | cut -d' ' -f 1 \
                   | cut -f 2 \
                   | sed 's/.*\(android\.googlesource\.com\/\)//');

  # Project detection
  else
    gerrit_project=$(echo "${gerrit_remote}" \
                   | cut -f 2 \
                   | sed 's/.*\/\([^.]*\).* .*/\1/');
    if [ ! "${gerrit_organisation}" = '.' ]; then
      gerrit_project=${gerrit_organisation}/${gerrit_project};
    fi;
  fi;

  # Check review statistics
  git fetch "${gerrit_url}/${gerrit_project}" "refs/heads/${gerrit_branch}";
  echo '';
  echo -n ' [Gerrit branch] New changes - missing : ';
  git rev-list --left-right --count HEAD...FETCH_HEAD;

  # Command creation
  local command="git push ${gerrit_url}/${gerrit_project} HEAD:refs/${gerrit_type}/${gerrit_branch}";

  # Command confirmation
  echo '';
  echo " ${command}";
  echo -n '  > Proceed with review upload (y/N/t) ? ';
  read -r key;

  # Set a topic
  if [ "${key}" = 't' ] || [ "${key}" = 'T' ]; then
    echo -n '  > Topic to set on gerrit : ';
    read -r key;
    command=${command}/${key};
    key=y;
  fi;

  # Abort if denied
  if [ ! "${key}" = 'y' ] && [ ! "${key}" = 'Y' ]; then
    echo '';
    return;
  fi;

  # Command second confirmation
  if [ ! -z "${gerrit_confirm_twice}" ]; then
    echo -n '  > Are you sure (y/N) ? ';
    read -r key;
    echo '';

    # Abort if denied
    if [ ! "${key}" = 'y' ] && [ ! "${key}" = 'Y' ]; then
      echo '';
      return;
    fi;
  fi;

  # Command execution
  ${command};
  echo '';
}

# === Gerrit Username ===
function gerritusername()
{
  # Usage: gerritusername (Gerrit username getter)

  # Gerrit Credentials
  export GerritUsername='Username';
  export GerritHTTPUsername='Username';
  export GerritHTTPPassword='abcdefgh12345678';

  # Create .bash_android.gerrit.rc with the exports to override the credentials
  if [ -f "${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.bash_android.gerrit.rc" ]; then
    source "${ANDROID_DEVELOPMENT_SHELL_TOOLS_WORKSPACE:-${HOME}}/.bash_android.gerrit.rc";
  fi;

  # Warn about missing credentials
  if [ "${GerritUsername}" = 'Username' ]; then
    echo '' 1>&2;
    echo -e ' \e[1;31mError: Open android_gerrit.rc (in gerritusername) to see how to set your logins\e[0m' 1>&2;
    echo '' 1>&2;
  fi;

  # Return username
  echo "${GerritUsername}";
}

# === Gerrit LineageOS ===
alias gitpr='gerritreview ssh://$(gerritusername)@review.lineageos.org:29418 LineageOS for';
alias gitprh='gerritreview http://review.lineageos.org LineageOS for';
alias gitpr14='gerritreview ssh://$(gerritusername)@review.lineageos.org:29418 LineageOS for cm-14.1';
alias gitpr15='gerritreview ssh://$(gerritusername)@review.lineageos.org:29418 LineageOS for lineage-15.0';
alias gitprd='gerritreview ssh://$(gerritusername)@review.lineageos.org:29418 LineageOS drafts';
alias gitprdh='gerritreview http://review.lineageos.org LineageOS drafts';
alias gitpg='gerritreview ssh://$(gerritusername)@review.lineageos.org:29418 LineageOS heads';
alias gitpgh='gerritreview http://review.lineageos.org LineageOS heads';
alias gitprg='gitpr; gitpg';
alias gitprgy='echo -n "y\ny\n" | gitpr; echo -n "y\ny" | gitpg';

# === Gerrit AOSP ===
alias gitpraosp='gerritreview https://android.googlesource.com aosp for';
alias gitpraospma='gerritreview https://android.googlesource.com aosp for master';
alias gitprdaosp='gerritreview https://android.googlesource.com aosp drafts';
alias gitprdaospma='gerritreview https://android.googlesource.com aosp drafts master';

# === Gerrit AICP ===
alias gitpraicp='gerritreview ssh://$(gerritusername)@gerrit.aicp-rom.com:29418 AICP for n7.1';
alias gitprhaicp='gerritreview http://gerrit.aicp-rom.com AICP for n7.1';
alias gitprdaicp='gerritreview ssh://$(gerritusername)@gerrit.aicp-rom.com:29418 AICP drafts n7.1';
alias gitprdhaicp='gerritreview http://gerrit.aicp-rom.com AICP drafts n7.1';
alias gitpgaicp='gerritreview ssh://$(gerritusername)@gerrit.aicp-rom.com:29418 AICP heads n7.1';
alias gitpghaicp='gerritreview http://gerrit.aicp-rom.com AICP heads n7.1';
alias gitprgaicp='gitpraicp; gitpgaicp';
alias gitprghaicp='gitprhaicp; gitpghaicp';

# === Gerrit LegacyXperia ===
alias gitpr7='gerritreview ssh://radian.dc@review.msm7x30.org:29418 LegacyXperia for';
alias gitpr7h='gerritreview http://review.msm7x30.org LegacyXperia for';
alias gitpr7d='gerritreview ssh://radian.dc@review.msm7x30.org:29418 LegacyXperia drafts';
alias gitpr7dh='gerritreview http://review.msm7x30.org LegacyXperia drafts';

# === Gerrit Omni ===
alias gitpromni='gerritreview ssh://$(gerritusername)@gerrit.omnirom.org:29418 . for android-7.1';
alias gitprhomni='gerritreview http://gerrit.omnirom.org . for android-7.1';
alias gitprdomni='gerritreview ssh://$(gerritusername)@gerrit.omnirom.org:29418 . drafts android-7.1';
alias gitprdhomni='gerritreview http://gerrit.omnirom.org . drafts android-7.1';

# === Gerrit TWRP ===
alias gitprtwrp='gerritreview ssh://$(gerritusername)@gerrit.twrp.me:29418 . for android-6.0';
alias gitprdtwrp='gerritreview ssh://$(gerritusername)@gerrit.twrp.me:29418 . drafts android-6.0';
